<template>
  <el-card shadow="never">
    <!-- 头部 -->
    <div slot="header">
      <el-breadcrumb separator='>'>
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>订单管理</el-breadcrumb-item>
        <el-breadcrumb-item>订单列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!-- 主体内容 -->
    <!-- 表单 -->
    <el-form :model="params"
             ref="form"
             label-width="80px"
             :inline="true"
             size="normal">
      <el-form-item label="用户ID"
                    prop="user_id">
        <el-input v-model="params.user_id"></el-input>
      </el-form-item>
      <!-- 支付状态----------------------------------------------------------------- -->
      <el-form-item label="支付状态"
                    prop="pay_status">
        <el-select v-model="params.pay_status">
          <el-option value=""
                     label="全部状态"></el-option>
          <el-option value="1"
                     label="已支付"></el-option>
          <el-option value="0"
                     label="未支付"></el-option>
        </el-select>
      </el-form-item>
      <!-- 发货状态----------------------------------------------------------------- -->
      <el-form-item label="发货状态"
                    prop="is_send">
        <el-select v-model="params.is_send">
          <el-option value=""
                     label="全部状态"></el-option>
          <el-option value="1"
                     label="已发货"></el-option>
          <el-option value="0"
                     label="未发货"></el-option>
        </el-select>
      </el-form-item>
      <!-- 按钮 ------------------------------------------------------------------------>
      <el-form-item>
        <el-button type="primary"
                   @click="getOrdersList">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 ------------------------------------------------------------------------->
    <el-table :data="list"
              border>
      <el-table-column label="#"
                       type="index"></el-table-column>
      <el-table-column label="订单编号"
                       width="300"
                       prop="order_number"></el-table-column>
      <el-table-column label="订单价格"
                       width="145"
                       prop="order_price"></el-table-column>
      <el-table-column label="用户ID"
                       width="145"
                       prop="user_id"></el-table-column>
      <el-table-column label="支付状态"
                       width="145">
        <template slot-scope="scope">
          <el-tag type="success"
                  v-if="scope.row.pay_status==='1'">已付款</el-tag>
          <el-tag type="warning"
                  v-if="scope.row.pay_status==='0'">未付款</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否发货"
                       width="145">
        <template slot-scope="scope">
          <el-tag type="success"
                  v-if="scope.row.is_send==='是'">已发货</el-tag>
          <el-tag type="warning"
                  v-else>未发货</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="下单时间"
                       width="170">
        <template slot-scope="scope">
          {{scope.row.create_time | formate}}
        </template>
      </el-table-column>
      <el-table-column label='操作'
                       width="145">
        <!-- 操作栏按钮 -->
        <template slot-scope="scope">
          <!-- 修改 -->
          <el-button size="small"
                     type="success"
                     icon="el-icon-edit"
                     @click=" isAddshow = true "></el-button>
          <!-- 查看物流信息 -->
          <el-button @click="seeEpress(scope.row)"
                     size="small"
                     type="warning"
                     icon="el-icon-truck"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -------------------------------------------------------------------------------->
    <!-- 分页器 -->
    <!-- page-size 每页个数 -->
    <!-- total 总条数 -->
    <!-- current-page 当前页码数 -->
    <el-pagination background
                   @size-change="sizeChange"
                   @current-change="currentChange"
                   :page-size="params.pagesize"
                   :current-page="params.pagenum"
                   layout="sizes,prev, pager, next,->,total"
                   :page-sizes="[10,20,30,40,50]"
                   :total="total">
    </el-pagination>

    <!-- 修改收货地址的弹框 -------------------------------------------------------->
    <el-dialog title="修改收货地址"
               :visible.sync="isAddshow"
               width="30%">
      <!-- 表单 -->
      <el-form :model="addFormInfo"
               ref="form"
               :rules="addFormRules"
               label-width="80px"
               :inline="false"
               size="normal">
        <el-form-item label="省市县"
                      prop="city">
          <el-cascader :options="cityOptions"
                       v-model="addFormInfo.city">
          </el-cascader>

        </el-form-item>
        <!-- type="textarea" 多行文本输入框 -->
        <el-form-item label="详细地址"
                      prop="address">
          <el-input v-model="addFormInfo.address"
                    type="textarea"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer">
        <el-button @click="isAddshow = false">取消</el-button>
        <el-button type="primary">确定</el-button>
      </span>
    </el-dialog>

    <!-- 物流信息的弹框 -------------------------------------------------------->
    <el-dialog title=""
               :visible.sync="expressShow"
               width="50%"
               @close="expressList=[]">

      <el-row>
        <el-col :span="20"
                :offset="2">
          <!-- 时间线组件 -->
          <el-timeline>
            <el-timeline-item v-for="(item, index) in expressList"
                              :key="index"
                              :type="item.type"
                              :color="item.color"
                              :icon="item.icon"
                              :timestamp="item.ftime">
              {{item.context}}
            </el-timeline-item>
          </el-timeline>
        </el-col>
      </el-row>
      <span slot="footer">
        <el-button type="primary"
                   @click="expressShow=false">关闭</el-button>
        <el-button type="primary">确定</el-button>
      </span>
    </el-dialog>

  </el-card>
</template>

<script>
// 导入省市县数据
//这个是导入的省市县数据的插件，导入过后想要在组件里使用，必须先把这些数据的变量 cityOptions 放在data里面，才能被组件使用
import cityOptions from '../libs/citydata'
export default {
  data() {
    return {
      params: {
        //查询参数
        user_id: '',
        pagenum: 1,
        pagesize: 10,
        pay_status: '',
        is_send: ''
      },
      list: [], //订单数据列表
      total: 0,
      isAddshow: false,
      addFormInfo: {
        city: [], //省市县地区列表
        address: '' //详细地址
      },
      addFormRules: {
        city: [{ required: true, $message: '请选择地区', trigger: 'blur' }],
        address: [
          { required: true, $message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      cityOptions, //省市县数据这个是导入的省市县数据的插件，导入过后想要在组件里使用，必须先把这些数据的变量放在data里面
      //   物流信息弹框
      expressShow: false,
      expressList: [] //订单的物流信息
    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    // 获取订单数据列表
    async getOrdersList() {
      const { data: res } = await this.$axios.get('/orders', {
        params: this.params
      })

      // 失败
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)

      // 成功
      this.list = res.data.goods //获取列表
      this.total = res.data.total //获取总数
      //   console.log(this.list)
    },
    // 页码变化
    sizeChange(val) {
      console.log('一页展示' + val + '条数据')
      // 每页显示几条的那个下拉框改变，会触发此事件
      // 把每一页显示几条数据重新设置
      this.params.pagesize = val
      // 修改完成重新渲染
      this.getOrdersList()
    },
    currentChange(val) {
      console.log('当前处于第' + val + '页')
      // 切换第几页时触发
      // 修改 当前页码，也就是当前处于第几页
      this.params.pagenum = val
      // 修改完成重新渲染
      this.getOrdersList()
    },
    // 搜索重置
    reset() {
      this.$refs.form.resetFields()
      this.params.pagenum = 1
      this.getOrdersList()
    },

    // 查看物流信息
    seeEpress(val) {
      // 判断是否发货
      if (val.is_send !== '是') {
        // 通知弹框
        return this.$notify.warning({
          title: '注意',
          message: '订单未发货，暂无物流信息'
        })
      }

      //    let {data:res}= await this.$axios.get('/kuaidi/1106975712662')
      //   后台没有接口，所以自己模拟数据
      const res = {
        data: [
          {
            time: '2018-05-10 09:39:00',
            ftime: '2018-05-10 09:39:00',
            context: '已签收,感谢使用顺丰,期待再次为您服务',
            location: ''
          },
          {
            time: '2018-05-10 08:23:00',
            ftime: '2018-05-10 08:23:00',
            context:
              '[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件',
            location: ''
          },
          {
            time: '2018-05-10 07:32:00',
            ftime: '2018-05-10 07:32:00',
            context: '快件到达 [北京海淀育新小区营业点]',
            location: ''
          },
          {
            time: '2018-05-10 02:03:00',
            ftime: '2018-05-10 02:03:00',
            context:
              '快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]',
            location: ''
          },
          {
            time: '2018-05-09 23:05:00',
            ftime: '2018-05-09 23:05:00',
            context: '快件到达 [北京顺义集散中心]',
            location: ''
          },
          {
            time: '2018-05-09 21:21:00',
            ftime: '2018-05-09 21:21:00',
            context: '快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]',
            location: ''
          },
          {
            time: '2018-05-09 13:07:00',
            ftime: '2018-05-09 13:07:00',
            context: '顺丰速运 已收取快件',
            location: ''
          },
          {
            time: '2018-05-09 12:25:03',
            ftime: '2018-05-09 12:25:03',
            context: '卖家发货',
            location: ''
          },
          {
            time: '2018-05-09 12:22:24',
            ftime: '2018-05-09 12:22:24',
            context: '您的订单将由HLA（北京海淀区清河中街店）门店安排发货。',
            location: ''
          },
          {
            time: '2018-05-08 21:36:04',
            ftime: '2018-05-08 21:36:04',
            context: '商品已经下单',
            location: ''
          }
        ],
        meta: { status: 200, message: '获取物流信息成功！' }
      }
      // 失败
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      //   给0号索引的对象添加属性 type 值为 success
      res.data[0].type = 'success'
      //   给0号索引的对象添加属性 icon 值为 el-icon-position
      res.data[0].icon = 'el-icon-position'
      //   将获取到的数据赋值给 expressList
      this.expressList = res.data
      //   打开弹框
      this.expressShow = true
    }
  }
}
</script>

<style>
</style>